package com.andy.qhb.utils;

import java.util.concurrent.atomic.AtomicInteger;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.entity.ConfCommon;
import com.andy.qhb.game.GameConf;

public class IdGen {
	
	private AtomicInteger idGen;
	private int max = 0;
	private ConfCommon common;
	
	private static int step = 100;
	public IdGen(String key) {
		common = GameConf.getConf(key);
		if(common != null){
			String v = common.getValue();
			if(v != null){
				int initV = Integer.parseInt(v);
				max = initV + step;
				common.setValue(String.valueOf(max));
				DaoUtils.update(common);
				idGen = new AtomicInteger(initV);
				genId();
			}
		}
	}
	
	public Integer genId() {
		int id = idGen.incrementAndGet();
		if(id == max - 10){
			max = max+step;
			common.setValue(String.valueOf(max));
			DaoUtils.update(common);
		}
		
		return id;
	}
}

